package cases.boxes 
{
	import com.greensock.easing.Strong;
	import com.greensock.plugins.MotionBlurPlugin;
	import com.greensock.plugins.ShakeEffect;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import org.despair2D.control.ZMouseEvent;
	import org.despair2D.Despair;
	import org.despair2D.ui.DespairUI;
	import org.despair2D.ui.core.IComponent;
	import org.despair2D.ui.Fusion;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.Persona;
	import org.despair2D.ui.PersonaState;
	import org.despair2D.ui.puppet.AnimePuppet;
	//import org.despair2D.utils.DespairUtil;
	//import org.despair2D.utils.schizoid.ITimer;
	import org.despair2D.control.MouseManager


public class AnimeTestBox extends PersonaState 
{

	override public function enter() : void
	{
		//var persona:Persona = new Persona()
		//persona.setState(new BDemoBox)
		//this.group.addFusion(persona)
		this.group.addEventListener(ManipulateEvent.PRESS, function(e:ManipulateEvent):void
		{
			trace('[AnimeTestBox] - Press Group')
		})
		
		this.group.move(90, 30);
		
		const LEN:int = 625;
		
		
		
		var A:AnimePuppet;
		var l:int = LEN;
		var i:int
		for (i = 0; i < l; i++)
		{
			F = new Fusion()
			A = F.createAnimePuppet('colonist_32_32');
			this.group.addFusion(F);
			F.x = (i % 25) * 31
			F.y = int(i / 25) * 18
			A.createActionReaction('right_run', null, true).play();
			//trace(new Point(F.x, F.y))
			F.addEventListener(ManipulateEvent.PRESS, pppp)
			//F.addEventListener(ManipulateEvent.RELEASE,r)
		}
		//this.createRawMoviePuppet('colonist_32_32',true)
		//F.interactive = false
		
		MouseManager.getInstance().leftButton.addEventListener(ZMouseEvent.MOUSE_RELEASE, rrrr, false, 20000)
		MouseManager.getInstance().addEventListener(ZMouseEvent.MOUSE_MOVE, onMove, false, 1000)
		TweenPlugin.activate([MotionBlurPlugin])
		
		TweenLite.from(this.group.displayObject, 1,{x:1000, motionBlur:{strength:2, quality:1}, ease:Strong.easeOut})
		//Despair.process.addUpdateListener(u)
	}
	
	override public function exit():void
	{
		MouseManager.getInstance().leftButton.removeEventListener(ZMouseEvent.MOUSE_RELEASE,rrrr)
		MouseManager.getInstance().removeEventListener(ZMouseEvent.MOUSE_MOVE, onMove)
		TweenLite.killTweensOf(this.group.displayObject)
	}
	
	
	private var F:Fusion
	//private var timer:ITimer
	
	private function pppp(e:ManipulateEvent):void
	{
		F = e.target as Fusion
		F.startDrag(false)//,new Rectangle(200,200,500,300))
		trace('[AnimeTestBox] - Fusion ? ' + F.name)
		//timer = DespairUtil.frameTimer.addTimer(0.3, 0).addRoundListener()
	}
	private function rrrr(e:ZMouseEvent):void
	{
		if (F)
		{
			F = null
			DespairUI.stopDrag()
		}
		
	}
	
	private function onMove(e:ZMouseEvent):void
	{
		if (F)
		{
			var puppet:AnimePuppet = this.group.createAnimePuppet('colonist_32_32', F.layer)
			var sourceP:AnimePuppet = F.getPuppetAt() as AnimePuppet
			puppet.pointer = sourceP.pointer
			var gp:Point = sourceP.globalPos
			puppet.moveInStage(gp.x, gp.y)
			puppet.addEventListener(ManipulateEvent.KILLED,onKilled)
			TweenLite.to(puppet.displayObject, 2, { alpha:0, onComplete:function():void
			{
				puppet.kill()
			}})
			
		}
	}
	
	private function onKilled(e:ManipulateEvent):void
	{
		var puppet:AnimePuppet = e.target as AnimePuppet
		TweenLite.killTweensOf(puppet.displayObject)
	}
	
}

}